package Shop;
import MannageService.Manage;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebServlet(name="ShopServlet",value ="/ShopServlet")
public class ShopServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(1112);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取表单提交的数据
        // 设置HttpServletResponse使用utf-8编码
        resp.setCharacterEncoding("utf-8");
// 通知浏览器使用utf-8解码
        resp.setHeader("Content-Type","text/html;charset=utf-8");
//        resp.setContentType("text/html;charset=utf-8");
        System.out.println("你来了?");
        //按钮信息获取
        String submit = req.getParameter("submit");
        HttpSession session = req.getSession();
        // 从session中获取值
        //String value = (String) session.getAttribute("key");
        //System.out.println("Session value: " + value);

        System.out.println("2222222");
        System.out.println("333333");



        // 数据库操作
        if (submit!=null){
            String product = new String(req.getParameter("product").getBytes("ISO-8859-1"),"UTF-8");//获取商品名字
            String imageUrl =new String(req.getParameter("image").getBytes("ISO-8859-1"),"UTF-8") ;//商品url即图片
            int quantity = Integer.parseInt(req.getParameter("quantity"));//数量
            //   float price = float.parseDouble(req.getParameter("price"));
            float price = Float.parseFloat(req.getParameter("price"));//价格
            String type= new String(req.getParameter("type").getBytes("ISO-8859-1"),"UTF-8");//类U型
            System.out.println("进入按钮");

            ShopDaoImp s=new ShopDaoImp();
            Manage manage= (Manage) session.getAttribute("manage");//获取服务器存储的管理员名字
            //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            if (manage == null) {
                manage = new Manage();
                // 避免直接运行时,没有通过login页面进行的登录储存用户然后报500错误,所以我给他增加了异常处理
                manage.setMname("游客");
                manage.setPassword("1111");
            }//测试用的,可删可不删,就是没有登录直接进入这个页面时由于session没有储存管理员这个用户无法获取到,然后代码就会报500
              //然后我赋值一个给他!!!!!!!!!!!
            //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!



            s.createshop(product,imageUrl,type,quantity,price,manage.getMname());
            System.out.println("上架成功");
            // 重定向到商品页面
            resp.sendRedirect("Shop.jsp");
        }else{
            Shop s1= (Shop) session.getAttribute("shop1");
            String button1=req.getParameter(s1.getProduct());
            System.out.println("进入删除按钮");
            ShopDaoImp s=new ShopDaoImp();
            s.deleteshop(s1.product);
            System.out.println("删除成功");
            // 重定向到商品页面
            resp.sendRedirect("Shop.jsp");

        }
    }
}
